home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / zipextimporter.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  4.0 KB  |  131 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''zipextimporter - an importer which can import extension modules from zipfiles
  5.  
  6. This file and also _memimporter.pyd is part of the py2exe package.
  7.  
  8. Overview
  9. ========
  10.  
  11. zipextimporter.py contains the ZipExtImporter class which allows to
  12. load Python binary extension modules contained in a zip.archive,
  13. without unpacking them to the file system.
  14.  
  15. Call the zipextimporter.install() function to install the import hook,
  16. add a zip-file containing .pyd or .dll extension modules to sys.path,
  17. and import them.
  18.  
  19. It uses the _memimporter extension which uses code from Joachim
  20. Bauch\'s MemoryModule library.  This library emulates the win32 api
  21. function LoadLibrary.
  22.  
  23. Sample usage
  24. ============
  25.  
  26. You have to prepare a zip-archive \'lib.zip\' containing
  27. your Python\'s _socket.pyd for this example to work.
  28.  
  29. >>> import zipextimporter
  30. >>> zipextimporter.install()
  31. >>> import sys
  32. >>> sys.path.insert(0, "lib.zip")
  33. >>> import _socket
  34. >>> print _socket
  35. <module \'_socket\' from \'lib.zip\\_socket.pyd\'>
  36. >>> _socket.__file__
  37. \'lib.zip\\\\_socket.pyd\'
  38. >>> _socket.__loader__
  39. <ZipExtensionImporter object \'lib.zip\'>
  40. >>> # Reloading also works correctly:
  41. >>> _socket is reload(_socket)
  42. True
  43. >>>
  44.  
  45. '''
  46. import imp
  47. import sys
  48. import zipimport
  49. import _memimporter
  50.  
  51. class ZipExtensionImporter(zipimport.zipimporter):
  52.     _suffixes = _[1]
  53.     
  54.     def find_module(self, fullname, path = None):
  55.         result = zipimport.zipimporter.find_module(self, fullname, path)
  56.         if result:
  57.             return result
  58.         
  59.         if fullname in ('pywintypes', 'pythoncom'):
  60.             fullname = fullname + '%d%d' % sys.version_info[:2]
  61.             fullname = fullname.replace('.', '\\') + '.dll'
  62.             if fullname in self._files:
  63.                 return self
  64.             
  65.         else:
  66.             fullname = fullname.replace('.', '\\')
  67.             for s in self._suffixes:
  68.                 if fullname + s in self._files:
  69.                     return self
  70.                     continue
  71.             
  72.  
  73.     
  74.     def locate_dll_image(self, name):
  75.         if name in self._files:
  76.             return self.get_data(name)
  77.         
  78.  
  79.     
  80.     def load_module(self, fullname):
  81.         if sys.modules.has_key(fullname):
  82.             mod = sys.modules[fullname]
  83.             if _memimporter.get_verbose_flag():
  84.                 sys.stderr.write('import %s # previously loaded from zipfile %s\n' % (fullname, self.archive))
  85.             
  86.             return mod
  87.         
  88.         _memimporter.set_find_proc(self.locate_dll_image)
  89.         
  90.         try:
  91.             return zipimport.zipimporter.load_module(self, fullname)
  92.         except zipimport.ZipImportError:
  93.             pass
  94.  
  95.         initname = 'init' + fullname.split('.')[-1]
  96.         filename = fullname.replace('.', '\\')
  97.         if filename in ('pywintypes', 'pythoncom'):
  98.             filename = filename + '%d%d' % sys.version_info[:2]
  99.             suffixes = ('.dll',)
  100.         else:
  101.             suffixes = self._suffixes
  102.         for s in suffixes:
  103.             path = filename + s
  104.             if path in self._files:
  105.                 if _memimporter.get_verbose_flag():
  106.                     sys.stderr.write('# found %s in zipfile %s\n' % (path, self.archive))
  107.                 
  108.                 code = self.get_data(path)
  109.                 mod = _memimporter.import_module(code, initname, fullname, path)
  110.                 mod.__file__ = '%s\\%s' % (self.archive, path)
  111.                 mod.__loader__ = self
  112.                 if _memimporter.get_verbose_flag():
  113.                     sys.stderr.write('import %s # loaded from zipfile %s\n' % (fullname, mod.__file__))
  114.                 
  115.                 return mod
  116.                 continue
  117.         
  118.         raise zipimport.ZipImportError, "can't find module %s" % fullname
  119.  
  120.     
  121.     def __repr__(self):
  122.         return '<%s object %r>' % (self.__class__.__name__, self.archive)
  123.  
  124.  
  125.  
  126. def install():
  127.     '''Install the zipextimporter'''
  128.     sys.path_hooks.insert(0, ZipExtensionImporter)
  129.     sys.path_importer_cache.clear()
  130.  
  131.